<template>
    <view class="content">
        <view class="item" v-for="(item, index) in list" :key="index" @click="choose(item)">
            <image :src="item.image" class="avatar"></image>
            <view class="u-flex-1">
                <view class="item-r-t">
                    <view class="name">{{item.name}}</view>
                    <view class="real">已实名</view>
                    <u-icon name="star-fill" color="#fec427" size="28"></u-icon>
                    <view class="star-num">{{item.star}}</view>
                </view>
                <view class="great">擅长：{{item.be_good_at}}</view>
            </view>
            <view class="item-r-r">
                <image v-if="item.checked" :src="IMAGEURL + 'static/icon/choose_01.png'" class="choose-01"></image>
                <image v-if="!item.checked" :src="IMAGEURL + 'static/icon/choose_02.png'" class="choose-01"></image>
                <view class="btn" @click.stop="$gTo(`/mine/pages/service/service-details?id=${item.user_id}`)">查看详情</view>
            </view>
        </view>
        
        <view style="height: 94rpx;padding-top: 30rpx;">
            <u-loadmore :status="status" v-if="status != 'loadmore'" />
        </view>
        
        <view class="footer">
            <view class="f-left" @click="allChoose">
                <image v-if="isChecked" :src="IMAGEURL + 'static/icon/choose_01.png'" class="choose-02"></image>
                <image v-if="!isChecked" :src="IMAGEURL + 'static/icon/choose_02.png'" class="choose-02"></image>
                <view class="u-p-l-10">全选</view>
            </view>
            <view class="del" @click="delList">删除</view>
        </view>
    </view>
</template>

<script>
    import { IMAGEURL } from '@/static/js/common.js';
    export default {
        data() {
            return {
                IMAGEURL,
                status: 'loadmore', //是否显示没有更多了
                page: 1,
                list: [],
                isChecked: false,
            };
        },
        onLoad() {
            this.getList()
        },
        onReachBottom() {
        	if(this.page == 1){return}
        	this.getList()
        },
        methods: {
            // 选中每一个项目
            choose(item) {
                var sellectList = []
                item.checked = !item.checked;
                this.list.forEach((el, index) => {
                    if (el.checked) {
                        sellectList.push(el)
                    }
                })
                if (sellectList.length == this.list.length) {
                    this.isChecked = true
                } else {
                    this.isChecked = false
                }
            },
            		
            // 全选
            allChoose() {
                this.isChecked = !this.isChecked
                if (this.isChecked) {
                    this.list.forEach((item, index) => {
                        item.checked = true
                    })
                } else {
                    this.list.forEach((item, index) => {
                        item.checked = false
                    })
                }
            },
            		
            // 删除收藏列表
            delList() {
                var ids = [];
                this.list.forEach(el => {
                    if (el.checked) {
                        ids.push(el.id)
                    }
                })
                if (ids.length == 0) {
                    this.$toast('请选择项目');
                    return false;
                }
                this.$ajax('delete_collect_browse', {
                    user_token: this.$getSync('userToken'),
                    ids: ids.join(','),
                }).then(ret => {
                    if (ret.success == 1000) {
                        this.isChecked = false
                        
                        this.page = 1
                        this.status = 'loadmore'
                        this.list = []
                        this.getList()
                    } else {
                        this.$toast(ret.msg);
                    }
                });
            },
            
            // 获取列表
            getList() {
            	if (this.status == 'nomore') return;
            	this.status = 'loading';
            	this.$ajax('user_collect', {
            		user_token: this.$getSync('userToken'),
            		page: this.page,
            		limit: 10,
            	}).then(ret => {
            		if (ret.success == 1000) {
            			if (ret.detail && ret.detail.collect && ret.detail.collect.length > 0) {
            				this.list = this.list.concat(ret.detail.collect);
            				this.page++;
            				this.status = 'loadmore'
                            
                            this.list.forEach(cur => {
                                this.$set(cur, 'checked', false)
                            })
            			} else {
            				this.status = 'nomore'
            			}
            		} else {
            			this.$toast(ret.msg);
            		}
            	});
            },
        }
    };
</script>

<style lang="scss">
    
    page{
        background-color: #f5f5f5;
    }
    .content{
        padding: 20rpx 20rpx 100px;
    }
    .item{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 30rpx 20rpx;
        background-color: #fff;
        border-radius: 20rpx;
        margin-bottom: 20rpx;
    }
    .avatar{
        width: 95rpx;
        height: 95rpx;
        border-radius: 50%;
        margin-right: 20rpx;
    }
    .item-r-t{
        display: flex;
        justify-content: flex-start;
        align-items: center;
    }
    .name{
        max-width: 250rpx;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 32rpx;
        font-weight: bold;
    }
    .real{
        padding: 7rpx 8rpx;
        background-color: #000;
        border-radius: 6rpx;
        font-size: 20rpx;
        color: #feeca8;
        line-height: 1;
        margin: 0 10rpx;
    }
    .star-num{
        font-size: 28rpx;
        font-weight: bold;
        padding-left: 6rpx;
    }
    .great{
        font-size: 24rpx;
        color: #949494;
        padding-top: 10rpx;
        width: 400rpx;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .item-r-r{
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
        align-items: flex-end;
    }
    .choose-01{
        width: 31rpx;
        height: 31rpx;
    }
    .choose-02{
        width: 33rpx;
        height: 33rpx;
    }
    .btn{
        font-size: 25rpx;
        color: #fff;
        padding: 12rpx 16rpx;
        background-color: #f29700;
        border-radius: 10rpx;
        margin-top: 25rpx;
    }
    
    .footer{
        width: 100vw;
        height: 100rpx;
        background-color: #fff;
        padding: 0 25rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 24rpx;
        position: fixed;
        bottom: 0;
        left: 0;
    }
    .f-left{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .del{
        font-size: 28rpx;
        color: #fff;
        padding: 18rpx 70rpx;
        background-color: #f29700;
        border-radius: 50rpx;
    }

</style>
